/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import com.sun.lwuit.Form;
import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import view.additional.ChernovtsiCreator;
import view.additional.Creator;
import view.additional.DefaultCreator;

/**
 *
 * @author Kvest
 */
public class OrderDetails extends Form implements ActionListener
{
    public static final int EMPTY_ID = 0;
    public static final int GET_ID = 1;
    public static final int BACK_ID = 2;
    public static final int FUNCTIONS_ID = 3;
    public static final int TO_CURRENT_ID = 4;
    public static final int MESSAGES_ID = 5;
    public static final int CANCEL_ID = 6;
    public static final int FAST_TAKE_ID = 7;

    private Command back;
    private Command get;
    private Command functions;
    private Command toCurrentOrder;
    private Command messages;
    private Command cancel;
    private Command fastTake;
    
    private FormAction listener;

    private Creator creator;  

    public OrderDetails(FormAction listener)
    {
        super("Детали заказа");
        this.listener = listener;
        int menuType = ViewManager.getViewManager().getFunctionMenuType(); 
        if (menuType == FunctionMenu.CHERNOVTSI_TYPE)
        {
            creator = new ChernovtsiCreator(this, false);
        }
        else
        {
            creator = new DefaultCreator(this);
        }
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addCommandListener(this);
        
        back = new Command("Назад",BACK_ID);
        get = new Command("Взять",GET_ID);
        fastTake = new Command(ViewManager.getViewManager().getTenMinutesText() + "мин.", FAST_TAKE_ID);
        functions = new Command("Функции",FUNCTIONS_ID);
        toCurrentOrder = new Command("К текущему",TO_CURRENT_ID);
        messages = new Command("Сообщения",MESSAGES_ID);
        cancel = new Command("Отмена",CANCEL_ID);

        creator.init();        
    }
    
    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            listener.commandAction(evt.getCommand().getId());
        }
    }

    public void setText(String value)
    {
        creator.setText(value);       
    }
    
    public void setCommands(boolean allCommands, boolean canGet, boolean isCurrent)
    {
        removeAllCommands();

        if (allCommands && canGet)
        {
            addCommand(fastTake);
        }
        else
        {
            addCommand(new Command("", EMPTY_ID));
        }
        addCommand(back);

        if (allCommands)
        {
            //Добавляем кнопку заказа
            if (canGet)
            {
                if (ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.DONETSK_TYPE)
                {
                    addCommand(messages);
                }
                addCommand(get);
            }
            else
            {
                if (ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.DONETSK_TYPE)
                {
                    if (ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.CHERNOVTSI_TYPE)
                    {
                        addCommand(cancel);
                    }
                    addCommand(messages);
                }
                if (isCurrent)
                {
                    addCommand(functions);
                }
                else
                {
                    addCommand(toCurrentOrder);
                }
            }
        }
    }
}
